local args = ngx.req.get_uri_args()

local act_id = args.act_id
local user_id = args.uid
local redis_client = require "lib.redis"
local redis = redis_client:new()

--开始投票
local rules, err = redis:hmget('rules:'..act_id, 'limit_type', 'limit_times')

if err ~= nil then
    ngx.say("0")
    return
end

local mode = rules[1]
local limit_times = tonumber(rules[2])
local times_key = "times:"..act_id..":"..user_id
local surplus_times = 0

if mode == 'day' then
    local times_of_everday = redis:hget(times_key, ngx.today())
    local t = tonumber(times_of_everday) or 0
    surplus_times = limit_times - t
else
    local times_of_life = redis:hget(times_key, 'total')
    local t = tonumber(times_of_life) or 0
    surplus_times = limit_times - t
end

ngx.say(surplus_times)